<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="校区编号" prop="deptId" >
        <a-tree-select
              v-model="form.deptId"
              style="width: 100%"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              :tree-data="treeData"
              placeholder="请选择校区编号"
              :replaceFields="{children:'children', title:'label', key:'id', value: 'id' }	"
              tree-default-expand-all
            >
          </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="班级类型" prop="scheduleType" >
        <a-select placeholder="请选择班级类型" v-model="form.scheduleType">
          <a-select-option v-for="(d, index) in scheduleTypeOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="上课日期" prop="week" >
        <a-select placeholder="请选择上课日期" v-model="form.week">
          <a-select-option v-for="(d, index) in weekOptions" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="上课开始时间" prop="start" >
        <a-time-picker format="HH:mm" v-model="form.start" valueFormat="HH:mm"/>
      </a-form-model-item>
      <a-form-model-item label="上课结束时间" prop="end" >
        <a-time-picker  format="HH:mm" v-model="form.end" valueFormat="HH:mm"/>
      </a-form-model-item>
      <a-form-model-item label="状态" prop="status" >
        <a-radio-group v-model="form.status" button-style="solid">
          <a-radio-button v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getScheduleTemplate, addScheduleTemplate, updateScheduleTemplate } from '@/api/manager/scheduleTemplate'


export default {
  name: 'CreateForm',
  props: {
    scheduleTypeOptions: {
      type: Array,
      required: true
    },
    weekOptions: {
      type: Array,
      required: true
    },
    statusOptions: {
      type: Array,
      required: true
    },treeData:{
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        deptId: null,

        scheduleType: null,

        week: null,

        start: null,

        end: null,

        status: '0',

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        deptId: [
          { required: true, message: '校区编号i不能为空', trigger: 'change' }
        ],

        scheduleType: [
          { required: true, message: '班级类型不能为空', trigger: 'change' }
        ],

        week: [
          { required: true, message: '上课日期不能为空', trigger: 'change' }
        ],

        start: [
          { required: true, message: '上课开始时间不能为空', trigger: 'blur' }
        ],

        end: [
          { required: true, message: '上课结束时间不能为空', trigger: 'blur' }
        ],

        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        deptId: null,

        scheduleType: null,

        week: null,

        start: null,

        end: null,

        status: '0',

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (deptId) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
      this.form.deptId = deptId
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getScheduleTemplate(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateScheduleTemplate(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addScheduleTemplate(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
